package com.free4inno.knowledgems.service;

import com.alibaba.fastjson.JSON;
import com.free4inno.knowledgems.dao.*;
import com.free4inno.knowledgems.domain.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Author HUYUZHU.
 * Date 2021/3/27 23:06.
 */

@Slf4j
@Service
public class ResourceService {

    @Autowired
    private ResourceDao resourceDao;

    @Autowired
    private GroupInfoDao groupInfoDao;

    @Autowired
    private LabelInfoDao labelInfoDao;

    @Autowired
    private SpecResourceDao specResourceDao;

    @Autowired
    private AttachmentDao attachmentDao;

    // 取群组名
    public String getGroupName(Resource resource) {
        log.info(this.getClass().getName() + "----in----" + "取资源群组名" + "----");
        String groupName = "";
        if (resource.getGroupId() != null && !resource.getGroupId().equals("")) {
            String[] group = resource.getGroupId().split(",|，");
            for (String s : group) {
                int groupId = (s == "" ? 0 : Integer.parseInt(s));

                // 1. JPA return Optional : findById
                // GroupInfo groupInfo = groupInfoDao.findById(groupId).orElse(new GroupInfo());

                // 2. manual turn to Optional : findAllById
                Optional<GroupInfo> groupInfo1 = Optional.of(groupInfoDao.findAllById(groupId));
                GroupInfo groupInfo = groupInfo1.orElse(new GroupInfo());

                if (groupInfo.getGroupName() != null) {
                    String gn = groupInfo.getGroupName();
                    groupName = groupName + "," + gn;
                } else {
                    groupName = groupName + "," + "未知群组" + groupId;
                }
            }
            groupName = groupName.substring(1);
        } else {
            groupName = "该资源无群组";
        }
        log.info(this.getClass().getName() + "----out----" + "返回群组名" + "----");
        return groupName;
    }

    // 取标签名
    public String getLabelName(Resource resource) {
        log.info(this.getClass().getName() + "----in----" + "取资源标签名" + "----");
        String labelName = "";
        if (resource.getLabelId() != null && !resource.getLabelId().equals("")) {
            String[] label = resource.getLabelId().split(",|，");
            for (String s : label) {
                int labelId = (s == "" ? 0 : Integer.parseInt(s));

                // 1. JPA return Optional : findById
                // LabelInfo labelInfo = labelInfoDao.findById(labelId).orElse(new LabelInfo());

                // 2. manual turn to Optional : findAllById
                Optional<LabelInfo> labelInfo1 = Optional.of(labelInfoDao.findAllById(labelId));
                LabelInfo labelInfo = labelInfo1.orElse(new LabelInfo());

                if (labelInfo.getLabelName() != null) {
                    String ln = labelInfo.getLabelName();
                    labelName = labelName + "," + ln;
                } else {
                    labelName = labelName + "," + "未知标签" + labelId;
                }
            }
            labelName = labelName.substring(1);
        } else {
            labelName = "该资源无标签";
        }
        log.info(this.getClass().getName() + "----out----" + "返回标签名" + "----");
        return labelName;
    }

    // 取附件
    public String getAttachmentJson(Integer resourceId) {
        log.info(this.getClass().getName() + "----in----" + "取资源附件" + "----");
        List<Attachment> attachmentList = attachmentDao.findAllByResourceId(resourceId);
        String attachmentJson = "[]";
        if (attachmentList != null && attachmentList.size() != 0) {
            int i = 0;
            ArrayList<Attachment> attachmentListNew = new ArrayList<>();
            for (Attachment attachment : attachmentList) {
                attachment.setId(i);
                attachmentListNew.add(attachment);
                i++;
            }
            attachmentJson = JSON.toJSONString(attachmentListNew);
        }
        log.info(this.getClass().getName() + "----out----" + "返回JSON格式的附件列表" + "----");
        return attachmentJson;
    }


    // 设置资源的推荐情况
    public void setSpecResource1(Resource resource) {
        log.info(this.getClass().getName() + "----in----" + "设置资源的推荐情况" + "----");
        Optional<SpecResource> specResourceOptional = specResourceDao.findByResourceIdAndType(resource.getId(), 1);
        if (specResourceOptional.isPresent()) {
            SpecResource specResource = specResourceOptional.get();
            Integer recommendType = specResource.getType();
            Integer recommendOrder = specResource.getOrder();
            resource.setRecommendType(recommendType);
            resource.setRecommendOrder(recommendOrder);
        }
        log.info(this.getClass().getName() + "----out----" + "完成资源推荐设置" + "----");
    }

    // 取资源的公告情况
    public void setSpecResource2(Resource resource) {
        log.info(this.getClass().getName() + "----in----" + "设置资源公告情况" + "----");
        Optional<SpecResource> specResourceOptional2 = specResourceDao.findByResourceIdAndType(resource.getId(), 2);
        if (specResourceOptional2.isPresent()) {
            SpecResource specResource2 = specResourceOptional2.get();
            Integer noticeType = specResource2.getType();
            Integer noticeOrder = specResource2.getOrder();
            resource.setNoticeType(noticeType);
            resource.setNoticeOrder(noticeOrder);
        }
        log.info(this.getClass().getName() + "----out----" + "完成资源公告设置" + "----");
    }
}
